﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class NewEvent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["SelectedDate"] == null)
                Response.Redirect("Default.aspx");
            else
            {
                lbSelectedDate.Text = Convert.ToDateTime(Session["SelectedDate"]).ToString("dd/MM/yyyy");
                int[] hours = new int[23];
                int[] minutes = new int[59];
                for (int i = 0; i < hours.Length; i++)
                {
                    hours[i] = i + 1;
                }
                ddlHour.DataSource = hours;
                ddlHour.DataBind();
                for (int i = 0; i < 59; i++)
                {
                    minutes[i] = i + 1;
                }
                ddlMinute.DataSource = minutes;
                ddlMinute.DataBind();
                ddlDuration.DataSource = minutes;
                ddlDuration.DataBind();

                if (Request.QueryString["EventID"] != null)
                {
                    GetSelectedDiaryEvent();
                }
            }
        }
    }

    private void GetSelectedDiaryEvent()
    {
        string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "Select EventName, EventDescription, EventDate, EventDuration From DiaryEvent Where EventID = @1";
        cmd.Parameters.AddWithValue("@1", Convert.ToInt32(Request.QueryString["EventID"]));
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            reader.Read();
            txtEventName.Text = reader.GetString(0);
            txtDesc.Text = reader.GetString(1);
            ddlHour.SelectedValue = reader.GetDateTime(2).Hour.ToString();
            ddlMinute.SelectedValue = reader.GetDateTime(2).Minute.ToString();
            ddlDuration.SelectedValue = reader.GetInt32(3).ToString();
            reader.Close();
        }
        cmd.Dispose();
        conn.Close();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool isOK = SaveDiaryEvent();
        if (isOK)
            CustomValidator1.ErrorMessage = "Save Diary Event Successfull";
        else
            CustomValidator1.ErrorMessage = "Save Diary Event Fail";
        CustomValidator1.IsValid = false;
    }

    private bool SaveDiaryEvent()
    {
        try
        {
            DateTime selectedDate = Convert.ToDateTime(Session["SelectedDate"]);
            DateTime eventDate = new DateTime(selectedDate.Year,
                selectedDate.Month,
                selectedDate.Day,
                int.Parse(ddlHour.SelectedValue),
                int.Parse(ddlMinute.SelectedValue), 0);
            int diaryID = Convert.ToInt32(Session["DiaryID"]);
            bool isUpdate = !string.IsNullOrEmpty(Request.QueryString["EventID"]);
            string sql;
            if (isUpdate)
                sql = "Update DiaryEvent Set EventName = @1, EventDescription = @2, EventDate = @3, EventDuration = @4 Where EventID = @5";
            else
                sql = "Insert Into DiaryEvent (DiaryID,EventName,EventDescription,EventDate,EventDuration) Values(@1,@2,@3,@4,@5)";
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            if (isUpdate)
            {
                cmd.Parameters.AddWithValue("@1", txtEventName.Text);
                cmd.Parameters.AddWithValue("@2", txtDesc.Text);
                cmd.Parameters.AddWithValue("@3", eventDate);
                cmd.Parameters.AddWithValue("@4", Convert.ToInt32(ddlDuration.SelectedValue));
                cmd.Parameters.AddWithValue("@5", diaryID);
            }
            else
            {
                cmd.Parameters.AddWithValue("@1", diaryID);
                cmd.Parameters.AddWithValue("@2", txtEventName.Text);
                cmd.Parameters.AddWithValue("@3", txtDesc.Text);
                cmd.Parameters.AddWithValue("@4", eventDate);
                cmd.Parameters.AddWithValue("@5", Convert.ToInt32(ddlDuration.SelectedValue));
            }
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}
